package com.srwl.mytx.widget;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.srwl.mytx.R;
import com.srwl.mytx.utils.MoneyInputCheck;
import com.srwl.mytx.utils.Utils;


public class NumberInputDialog extends DialogFragment {
    private static final String TAG = "NumberInputDialog";

    private String title;
    private View rl_confirm, rl_cancel;

    private ConfirmListener confirmListener;
    private CancleListener cancleListener;
    private TextView tv_title;
    private EditText et_amount;
    private TextView tv_confirm;
    private TextView tv_errorMsg;
    private MoneyInputCheck moneyInputCheck;

    public static NumberInputDialog newInstance() {
        return new NumberInputDialog();
    }

    public static NumberInputDialog newInstance(String title, double minLimit) {

        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putDouble("minLimit", minLimit);
        NumberInputDialog dialog = new NumberInputDialog();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int theme = android.R.style.Theme_Holo_Light_Dialog;
        setStyle(DialogFragment.STYLE_NO_TITLE, theme);


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: 执行了，，，");
        View view = inflater.inflate(R.layout.layout_donation_dialog, container, false);
        tv_title = view.findViewById(R.id.tv_title);
        Bundle arguments = getArguments();
        Double minLimit = null;
        if (arguments != null) {
            String title = arguments.getString("title");
            tv_title.setText(title);
            minLimit = arguments.getDouble("minLimit");
        }
        et_amount = view.findViewById(R.id.et_donation_amount);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        tv_errorMsg = view.findViewById(R.id.tv_error_msg);
        rl_confirm = view.findViewById(R.id.rl_confirm);
        rl_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmListener != null) {
                    confirmListener.confirm(Double.valueOf(et_amount.getText().toString()));
                    dismiss();
                } else {
                    dismiss();
                }
            }
        });
        view.findViewById(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_amount.setText("");
            }
        });
        moneyInputCheck = new MoneyInputCheck(getActivity(), new MoneyInputCheck.InteractionListener() {
            @Override
            public void showErrorMessage(String message) {
                rl_confirm.setClickable(false);
                tv_confirm.setTextColor(getResources().getColor(R.color.placeholder));
                tv_errorMsg.setText(message);
                tv_errorMsg.setVisibility(View.VISIBLE);
              //  et_amount.setError(message);
                et_amount.setTextColor(getResources().getColor(R.color.rp_money_red_light));
            }

            @Override
            public void waitInput() {
                rl_confirm.setClickable(false);
                tv_confirm.setTextColor(getResources().getColor(R.color.placeholder));
                et_amount.setTextColor(getResources().getColor(R.color.rp_text_black));
                tv_errorMsg.setVisibility(View.GONE);
            }

            @Override
            public void inputRight() {
                rl_confirm.setClickable(true);
                et_amount.setTextColor(getResources().getColor(R.color.rp_text_black));
                tv_confirm.setTextColor(getResources().getColor(R.color.rp_text_black));
                tv_errorMsg.setVisibility(View.GONE);
            }
        });

        if (minLimit != null) {
            et_amount.setText(Utils.moneyFormat(minLimit));
            moneyInputCheck.setMinLimit(minLimit);
            et_amount.setHint("最小金额为 " + minLimit);
        }
        et_amount.setSelection(et_amount.getText().length());
        et_amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                et_amount.setTextColor(getResources().getColor(R.color.rp_text_black));
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                moneyInputCheck.onTextChanged(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                moneyInputCheck.afterTextChanged(s);
            }
        });
        rl_cancel = view.findViewById(R.id.rl_cancel);
        rl_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancleListener != null) {
                    cancleListener.cancle();
                    dismiss();
                } else {
                    dismiss();
                }
            }
        });
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow(); // 一定要设置Background，如果不设置，window属性设置无效
        // win.setBackgroundDrawable(new ColorDrawable(new ColorDrawable(Color.WHITE)));
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.CENTER; // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = (int) (dm.widthPixels - 20 * dm.density);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

//    @NonNull
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        Log.d(TAG, "onCreateDialog: 执行了");
//        Dialog dialog = new Dialog(getActivity());
//
//        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
//        dialog.setContentView(R.layout.layout_donation_dialog);
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//        dialog.setCanceledOnTouchOutside(true);
//
//        // 设置宽度为屏宽、在屏幕中的位置
//        Window window = dialog.getWindow();
//        // window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//        WindowManager.LayoutParams wlp = window.getAttributes();
//        wlp.gravity = Gravity.CENTER;
//        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
//        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        wlp.horizontalMargin=30;
//        window.setAttributes(wlp);
//        return dialog;
//    }

    //    public NumberInputDialog(@NonNull Context context, String title) {
//        super(context);
//        this.title = title;
//
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.layout_donation_dialog);
//        TextView tv_title = findViewById(R.id.tv_title);
//        tv_title.setText(title);
//         et_amount = findViewById(R.id.et_donation_amount);
//
//
//        rl_confirm = findViewById(R.id.rl_confirm);
//        rl_confirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (confirmListener != null) {
//                    confirmListener.confirm(Double.valueOf( et_amount.getText().toString()));
//                    dismiss();
//                } else {
//                    dismiss();
//                }
//            }
//        });
//
//        rl_cancle = findViewById(R.id.rl_cancle);
//        rl_cancle.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (cancleListener != null) {
//                    cancleListener.cancel();
//                    dismiss();
//                } else {
//                    dismiss();
//                }
//            }
//        });
//
//
//    }

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void setCancelListener(CancleListener cancleListener) {
        this.cancleListener = cancleListener;

    }

    public interface ConfirmListener {
        void confirm(Double amount);
    }

    public interface CancleListener {
        void cancle();
    }


}
